package com.validations;



import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;

import javax.crypto.BadPaddingException;

import javax.crypto.Cipher;

import javax.crypto.IllegalBlockSizeException;

import javax.crypto.spec.SecretKeySpec;

public class Encrypt 

{

	private static String algorithm = "DESede";

    //private static Key key = null;

	private static SecretKeySpec key = null;

    private static Cipher cipher = null;

	private static Encrypt obj = new Encrypt();     

	private Encrypt() 

	{        

		try 

		{

			setUp();

		} 

		catch (Exception e) 

		{

			System.err.println(e);

		}

	}  

	

	private static void setUp() throws Exception 

	{

		//key = KeyStore

		//key = KeyGenerator.getInstance(algorithm).generateKey();

		byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,

		        0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };



		key = new SecretKeySpec(keyBytes, algorithm);

        cipher = Cipher.getInstance(algorithm);

    }

	public Encrypt getInstance()

	{

		return obj;

	}

	public static byte[] encrypt(String input)

					    throws InvalidKeyException, 

					    	BadPaddingException,

					    	IllegalBlockSizeException, UnsupportedEncodingException 

    {

		cipher.init(Cipher.ENCRYPT_MODE, key);

		byte[] inputBytes = input.getBytes();;

		return cipher.doFinal(inputBytes);

    }



	public static String decrypt(byte[] encryptionBytes)

						throws InvalidKeyException, 

					    	BadPaddingException,

					    	IllegalBlockSizeException 

	{

		cipher.init(Cipher.DECRYPT_MODE, key);

		byte[] recoveredBytes = 

			cipher.doFinal(encryptionBytes);

		String recovered = 

			new String(recoveredBytes);

		return recovered;

	}

}

